<template>
  <!-- 头部区域 -->
  <el-container class="home-container">
    <el-header>
      <div class="header-left">
        <div class="header-img">
          <img src="../assets/logo.png" alt="">
        </div>
        <span>神鸡后台</span>
      </div>
      <div class="header-right">
        <div class="header-img">
          <img src="../assets/cat.jpg" alt="">
        </div>
        <span>{{loginAcct}}</span>
        <el-button type="danger" @click='logout'>退出系统</el-button>
      </div>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <el-aside :width="isCollaspe ? '64px' : '200px'">
        <div class="toggle-button" @click="toggle_collspace">||||</div>
        <el-menu
              style="width: 200px ;"
              background-color="#409EFF"
              text-color="#ffffff"
              active-text-color="#a7a7a7"
              :unique-opened="true"
              :collapse="isCollaspe"
              :collapse-transition="false"
              :router="true"
              :default-active="$route.path">
              <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id" style="background-color: #409EFF;">
                <template slot="title">
                  <i :class="item.icon"></i>
                  <span>{{item.name}}</span>
                </template>
                <el-menu-item :index="'/'+subItem.url" v-for="subItem in item.children" :key="subItem.id" style="background-color: #3990e6;">
                  <template slot="title">
                    <i :class="subItem.icon"></i>
                    <span>{{subItem.name}}</span>
                  </template>
                </el-menu-item>
              </el-submenu>
            </el-menu>
      </el-aside>
      <el-container>
        <el-main><router-view></router-view></el-main>
        <!-- 底部区域 -->
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created () {
    this.getMenuList()
  },
  data () {
    return {
      loginAcct: window.sessionStorage.getItem('loginAcct'),
      // 左侧菜单数据
      menuList: [],
      isCollaspe: false
    }
  },
  methods: {
    logout () {
      this.$http.get('logout', {
        params: {
          loginAcct: window.sessionStorage.getItem('loginAcct')
        }
      })
      window.sessionStorage.removeItem('token')
      window.sessionStorage.removeItem('loginAcct')
      this.$router.push('/login')
    },
    async getMenuList () {
      // 获取左边菜单栏数据
      const { data: res } = await this.$http.post('menus')
      if (res.result !== 'SUCCESS') {
        return this.$message.error(res.message)
      }
      this.menuList = res.data.menu
    },
    // 点击按钮折叠菜单
    toggle_collspace () {
      this.isCollaspe = !this.isCollaspe
    }
  }
}
</script>

<style lang="less" scope>
  .el-header {
    background-color: #409EFF;
    color: #FFF1F0;
    font-size: 20px;
    font-family: "Helvetica Neue";
  }
  .el-aside {
    background-color: #409EFF;
  }
  .el-main {
    background-color: #D9ECFF;
  }
  .el-footer {
    background-color: #909399;
    width: 100%;
    height: 30px !important;
    bottom: 0px;
    padding: 5px !important;
  }
  .home-container {
    height: 100%;
  }
  .header-left {
    float: left;
    height: 100%;
  }
  .header-img {
    height: 40px;
    width: 40px;
    float: left;
    border: 2px solid #3e82ff;
    border-radius: 20px;
    background-color: #dedede;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
    margin-left: 30px;
    margin-right: 30px;
  }
  .header-img >img {
    margin: auto auto;
    width: 100%;
    height: 100%;
    background-size: cover;
    border-radius: 20px;
  }
  .header-left > span {
    float: right;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
  }
  .header-right {
    display: flex;
    float: right;
    height: 100%;
  }
  .header-right > span {
    float: right;
    position: relative;
    top: 50%;
    transform: translateY(-20%);
    padding-right: 30px;
  }
  .header-right > .el-button {
    height: 80%;
    margin: auto auto;
  }
  .toggle-button {
    background-color: #4681ff;
    color: #FFFFAA;
    line-height: 24px;
    font-size: 10px;
    text-align: center;
    letter-spacing: 2px;
    cursor: pointer;
  }
</style>
